<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>MyClouds：用户与授权：授权</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
		<style type="text/css">
			.m-box {
				line-height: 50px;
				text-align: center;
			}

			.left {
				float: left;
				width: 50%;
			}

			.right {
				margin-left: 50%;
				text-align: right;
			}

			.m-left {
				margin-right: 50px;
			}

			.m-right {
				margin-left: 50px;
			}

			.m-center {
				width: 100px;
				position: absolute;
				top: 40%;
				left: 50%;
				margin-left: -50px;
				z-index: 2;
			}
		</style>
	</head>
	<body>
		<div>
			<div class="left">
				<div class="m-box m-left">
					<table id="t-role-left" lay-filter="t-role-left"></table>
				</div>
			</div>
			<div class="m-box m-center">
				<button id="btn-grant" class="layui-btn layui-btn-primary layui-btn-sm">授权 <i class="layui-icon layui-icon-next"></i>
				</button>
				<button id="btn-cancel" class="layui-btn layui-btn-primary layui-btn-sm" style="margin-left:0px"><i class="layui-icon layui-icon-prev"></i>
					撤销
				</button>
			</div>
			<div class="right">
				<div class="m-box m-right">
					<table id="t-role-right" lay-filter="t-role-right"></table>
				</div>
			</div>
		</div>
	</body>
	<script src="../../layuiadmin/layui/layui.js"></script>
	<script type="text/javascript">
		layui.config({
			base: '../../layuiadmin/'
		}).extend({
			index: 'lib/index'
		}).use(['index', 'form', 'myutil', 'table'], function() {
			var $ = layui.$,
				myutil = layui.myutil,
				form = layui.form,
				table = layui.table;
			myutil.init();
			var userId = myutil.pathVar('id');
			var where = {
				userId: userId
			}

			table.render({
				id: 't-role-left',
				elem: '#t-role-left',
				url: layui.setter.my.surl + 'admin/system/user/listToGrantRoles',
				contentType: 'application/json',
				method: 'post',
				where: where,
				cols: [
						[{
						type: 'checkbox'
						}, {
						field: 'name',
						title: '待选角色（未授权）',
						align: 'left',
						minWidth: 100
						}]
					],
				page: false,
				limit: 10000,
				height: 'full-30',
				loading: true,
				text: {
					none: '没有符合条件的数据'
				}
			});

			table.render({
				id: 't-role-right',
				elem: '#t-role-right',
				url: layui.setter.my.surl + 'admin/system/user/listGrantedRoles',
				contentType: 'application/json',
				method: 'post',
				where: where,
				cols: [
						[{
						type: 'checkbox'
						}, {
						field: 'name',
						title: '已选角色（已授权）',
						align: 'left',
						minWidth: 100
						}]
					],
				page: false,
				limit: 10000,
				height: 'full-30',
				loading: true,
				text: {
					none: '没有符合条件的数据'
				}
			});

			function fnListTable() {
				table.reload('t-role-left', {
					where: where
				});
				table.reload('t-role-right', {
					where: where
				});
			}

			//授权
			$('#btn-grant').on('click', function() {
				var checkStatus = table.checkStatus('t-role-left');
				var total = checkStatus.data.length;
				if (total == 0) {
					layer.msg('请先在待选角色列表选中要授予的角色', {
						icon: 5
					});
					return;
				}
				layer.confirm('确定将选中的 ' + total + ' 个角色权限授予用户吗？', {
					icon: 3
				}, function(index) {
					myutil.ajax({
						url: layui.setter.my.surl + 'admin/system/user/grant',
						data: {
							userId: userId,
							roleIds: myutil.contactBy(checkStatus.data, 'id', ',')
						},
						ok: function(res) {
							layer.alert(res.msg, {
								icon: 1
							});
							fnListTable();
						}
					});
				});
			});

			//撤销
			$('#btn-cancel').on('click', function() {
				var checkStatus = table.checkStatus('t-role-right');
				var total = checkStatus.data.length;
				if (total == 0) {
					layer.msg('请先在已选角色列表选中要撤销的角色', {
						icon: 5
					});
					return;
				}
				layer.confirm('确定撤销选中的 ' + total + ' 个角色授予权限吗？', {
					icon: 3
				}, function(index) {
					myutil.ajax({
						url: layui.setter.my.surl + 'admin/system/user/cancel',
						data: {
							userId: userId,
							roleIds: myutil.contactBy(checkStatus.data, 'id', ',')
						},
						ok: function(res) {
							layer.alert(res.msg, {
								icon: 1
							});
							fnListTable();
						}
					});
				});
			});

		});
	</script>
</html>
